/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.model;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 *
 * @author orgale
 */
@Entity
@Table(name = "materials")
@NamedQueries({@NamedQuery(name = "Material.findAll", query = "SELECT m FROM Material m"),
    @NamedQuery(name = "Material.findByIdMaterial", query = "SELECT m FROM Material m WHERE m.id = :idMaterial"),
    @NamedQuery(name = "Material.findByName", query = "SELECT m FROM Material m WHERE m.name = :name"),
    @NamedQuery(name = "Material.findByUnitMeasure", query = "SELECT m FROM Material m WHERE m.unitMeasure = :unitMeasure"),
    @NamedQuery(name = "Material.findByState", query = "SELECT m FROM Material m WHERE m.state = :state")})
public class Material extends BaseObject implements Serializable {

    private static final long serialVersionUID = 1L;
    private Long id;
    private String name;
    private MeasureUnit unitMeasure;
    private Boolean state;
    private Collection<Material> materialCollection;
    private Material idMaterialRoot;
    private List<Distinctive> distinctives;
    private StorageMaterial storageMaterial;
    private List<PurchaseDetailCutProcess> listPurchaseDetailCutProcess;

    public Material() {
        unitMeasure = new MeasureUnit();
        distinctives = new ArrayList<Distinctive>();
    }

    public Material(Long idMaterial) {
        this.id = idMaterial;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id_material")
    @Basic(optional = false)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "name", nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @OneToOne
    public MeasureUnit getUnitMeasure() {
        return unitMeasure;
    }

    public void setUnitMeasure(MeasureUnit unitMeasure) {
        this.unitMeasure = unitMeasure;
    }

    @Column(name = "state")
    public Boolean getState() {
        return state;
    }

    public void setState(Boolean state) {
        this.state = state;
    }

//    public Collection<DetailMaterial> getDetailMaterialCollection() {
//        return detailMaterialCollection;
//    }
//
//    public void setDetailMaterialCollection(Collection<DetailMaterial> detailMaterialCollection) {
//        this.detailMaterialCollection = detailMaterialCollection;
//    }
//
//    public Collection<PurchaseDetail> getPurchaseDetailCollection() {
//        return purchaseDetailCollection;
//    }
//
//    public void setPurchaseDetailCollection(Collection<PurchaseDetail> purchaseDetailCollection) {
//        this.purchaseDetailCollection = purchaseDetailCollection;
//    }
    @OneToMany(mappedBy = "idMaterialRoot")
    public Collection<Material> getMaterialCollection() {
        return materialCollection;
    }

    public void setMaterialCollection(Collection<Material> materialCollection) {
        this.materialCollection = materialCollection;
    }

    @JoinColumn(name = "id_material_root", referencedColumnName = "id_material")
    @ManyToOne
    public Material getIdMaterialRoot() {
        return idMaterialRoot;
    }

    public void setIdMaterialRoot(Material idMaterialRoot) {
        this.idMaterialRoot = idMaterialRoot;
    }

    @OneToOne(cascade = CascadeType.REMOVE, mappedBy = "material")
    public StorageMaterial getStorageMaterial() {
        return storageMaterial;
    }

    public void setStorageMaterial(StorageMaterial storage) {
        this.storageMaterial = storage;
    }

    @ManyToMany
    @JoinTable(name = "material_distinctive",
    joinColumns = @JoinColumn(name = "id_material"),
    inverseJoinColumns = @JoinColumn(name = "id_distinctive"))
    public List<Distinctive> getDistinctives() {
        if (distinctives == null) {
            distinctives = new ArrayList<Distinctive>();
        }
        return distinctives;
    }

    public void setDistinctives(List<Distinctive> distinctives) {
        this.distinctives = distinctives;
    }

    //    public Collection<MaterialSupplier> getMaterialSupplierCollection() {
//        return materialSupplierCollection;
//    }
//
//    public void setMaterialSupplierCollection(Collection<MaterialSupplier> materialSupplierCollection) {
//        this.materialSupplierCollection = materialSupplierCollection;
//    }
//
//    public Collection<Delivery> getDeliveryCollection() {
//        return deliveryCollection;
//    }
//
//    public void setDeliveryCollection(Collection<Delivery> deliveryCollection) {
//        this.deliveryCollection = deliveryCollection;
//    }
    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "material")
    public List<PurchaseDetailCutProcess> getListPurchaseDetailCutProcess() {
        return listPurchaseDetailCutProcess;
    }

    public void setListPurchaseDetailCutProcess(List<PurchaseDetailCutProcess> listPurchaseDetailCutProcess) {
        this.listPurchaseDetailCutProcess = listPurchaseDetailCutProcess;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Material)) {
            return false;
        }
        Material other = (Material) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(this.getId()).append(this.name).append(this.unitMeasure).append(this.state).toString();
    }

    // this method dosn't of the bean
    @Transient
    public String getDescription() {
        if (getDistinctives().isEmpty()) {
            return "";
        } else {
            String result ="";
            for (int i = 0; i < getDistinctives().size(); i++) {
                //if (!getDistinctives().get(i).getName().matches("tipo material")) {
                    result += " " + getDistinctives().get(i).getValue();
                //}
            }
            
            return result;
        }

    }

    @Transient
    public String getFullName() {
        return name + " "+getDescription();
    }

    @Transient
    public String getCompleteName() {
        String result = name;
        for (Distinctive d : getDistinctives()) {
            if (!d.getName().matches("tipo material")) {
                result += " " + d.getValue();
            }
        }
        return result;
    }
}
